package ntd.util.kl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import ntd.util.UtilLocal;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class UtilKl {

    /**
     * 公众号支付
     * @param out_trade_no
     * @param total_fee
     * @param openid
     * @return
     */
    public static String unifiedorder(String out_trade_no, String total_fee, String openid) {
        final Map<String, Object> params = new TreeMap();
        params.put("txn_num", "101024");
        params.put("open_id", openid);
        final Map<String, Object> map = getResponse(out_trade_no, total_fee, params);
        final Object pay_info = map.get("pay_info");
        return JSONUtil.toJsonStr(pay_info);
    }

    /**
     * 扫码支付
     * @param out_trade_no
     * @param total_fee
     * @return
     */
    public static String unifiedsm(String out_trade_no, String total_fee) {
        final Map<String, Object> params = new TreeMap();
        params.put("txn_num", "101022");
        final Map<String, Object> map = getResponse(out_trade_no, total_fee, params);
        return JSONUtil.toJsonStr(map);
    }

    private static Map<String, Object> getResponse(String out_trade_no, String total_fee, Map<String, Object> params) {
        params.put("version", "2.0");
        params.put("date_time", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        params.put("mcht_id", "572100000022218");
        params.put("out_order_no", out_trade_no);
        params.put("nonce_str", RandomUtil.randomStringUpper(10));
        params.put("total_fee", total_fee);
        params.put("body", "宋词-酒智付");
        params.put("mch_create_ip", UtilLocal.getWebIp());

        params.put("sign", paySign(params, "p7ivj1hssyyvesud56wp6exkq70t1apt"));
        final String post = HttpUtil.post("http://woap.kalian168.com/AMPS/pay/gateway", JSONUtil.toJsonStr(params));
        Console.log(post);
        return JSONUtil.parseObj(post);
    }

    public static void main(String[] args) {
//        Console.log(unifiedorder("1351353", "1", "oaX2X5xLCTha56MxdGwX7c4GSvBc"));
        Console.log(unifiedsm("1351344543433", "1"));
    }



    /**
     * 签名方法
     * @param params
     * @return
     */
    public static String paySign(Map<String, Object> params, String key) {
        StringBuilder stringBuilder = new StringBuilder();
        params.entrySet().forEach(stringObjectEntry -> {
            stringBuilder.append("&");
            stringBuilder.append(stringObjectEntry.getKey());
            stringBuilder.append("=");
            stringBuilder.append(JSONUtil.toJsonStr(stringObjectEntry.getValue()));
        });
        stringBuilder.append("&");
        stringBuilder.append(key);
        String string = stringBuilder.toString();
        if (string.startsWith("&")) {
            string = string.substring(1);
        }
        return SecureUtil.md5(string).toUpperCase();
    }
}
